<?php

declare(strict_types=1);

namespace app\common\service;

use InvalidArgumentException;
use think\Manager;
use think\helper\Arr;

/**
 * Sms 短信管理类
 * @package app\common\service
 */
class Sms extends Manager
{
    protected $namespace = '\\app\\common\\service\\sms\\driver\\';


    /**
     * driver()的别名
     * @param string|array $name 渠道/通道名
     * @return mixed
     */
    public function channel(string|null $name = null)
    {
        return $this->driver($name);
    }

    /**
     * 发送短信
     * @param string $phone 手机号码
     * @param array $message 短信内容
     * @return mixed
     */
    // public function send(string $phone, array $message)
    // {
    //     return $this->channel()->send($phone, $message);
    // }

    /**
     * 默认驱动
     * @return string|null
     */
    public function getDefaultDriver()
    {
        return $this->getConfig('default');
    }

    protected function resolveType(string $name)
    {
        return $this->getChannelConfig($name, 'type', 'OnlyLog');
    }

    protected function resolveConfig(string $name)
    {
        return $this->getChannelConfig($name);
    }


    /**
     * 获取配置-自定义
     * @access public
     * @param null|string $name    名称
     * @param mixed       $default 默认值
     * @return mixed
     */
    public function getConfig(?string $name = null, $default = null)
    {
        if (!is_null($name)) {
            return $this->app->config->get('sms.' . $name, $default);
        }

        return $this->app->config->get('sms');
    }

    /**
     * 获取渠道配置-自定义
     * @param string $channel
     * @param string $name
     * @param mixed  $default
     * @return array
     */
    public function getChannelConfig(string $channel, ?string $name = null, $default = null)
    {
        if ($config = $this->getConfig("channels.{$channel}")) {
            return Arr::get($config, $name, $default);
        }

        throw new InvalidArgumentException("Channel [$channel] not found.");
    }
}
